﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using BusinessLogic.Interfaces;
using Domain;
using Domain.Entities;

namespace BusinessLogic.Implementations
{
     public  class EFRoleRepository:IRoleRepository
    {
         private EFDbContext context;
         public EFRoleRepository(EFDbContext context)
         {
             this.context = context;
         }
        
         public IEnumerable<Role> GetRoles()
         {
             return context.Roles.ToList();
         }

         public Role GetRoleById(int id)
         {
             return context.Roles.FirstOrDefault(i => i.Id == id);
         }

         public Role GetRoleByName(string roleName)
         {
             return context.Roles.FirstOrDefault(i => i.Name == roleName);
         }

         public void CreateRole(Role role)
         {
             context.Roles.Add(role);
             context.SaveChanges();
         }

         public void CreateRole(string role)
         {
            CreateRole( new Role{Name = role});
         }

         public void SaveRole(Role role)
         {
             if (role.Id == 0)
             {
                 context.Roles.Add(role);
             }
             else
             {
                 context.Entry(role).State = EntityState.Modified;
             }
             context.SaveChanges();
         }

          public void DeleteRoleById(int id)
          {
              context.Roles.Remove(GetRoleById(id));
              context.SaveChanges();
          }
    }
}
